<template>
  <div style="min-width: 1100px">
    <div class="licc-order-return-process-top">
      <el-form :model="filterOrderList" ref="filterOrderList"  label-width="80px">
        <el-row>
          <el-col :span="5">
            <el-form-item
              label=""
              prop="orderSn"
              :rules="[{trigger: 'blur', validator: validateOrderSnRule}]">
              <el-input v-model="filterOrderList.orderSn" placeholder="请输入订单号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item
              label=""
              prop="phone"
              :rules="[{trigger: 'blur', validator: validateOrderSnRule}]">
              <el-input v-model="filterOrderList.phone" placeholder="退货人手机号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="申请时间:">
              <el-date-picker
                v-model="datePickerValue"
                value-format="yyyy-MM-dd HH:mm:SS"
                :picker-options="pickerOptions"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4" :offset="2">
            <el-button type="info" @click="restOrderInfosButton">重 置</el-button>
            <el-button type="primary" @click="selectOrderInfosButton">筛 选</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div>
      <el-table
        stripe
        border
        :data="orderInfos">
        <el-table-column
          type="index"
          width="50">
        </el-table-column>
        <el-table-column prop="orderSn" label="订单编号" sortable></el-table-column>
        <el-table-column prop="invoiceNo" label="发货单号" sortable></el-table-column>
        <el-table-column prop="applayReturnTime" label="申请时间" sortable></el-table-column>
        <el-table-column prop="orderStatus" label="状态">
          <el-tag type="primary">处理中</el-tag>
        </el-table-column>
        <el-table-column prop="orderReturn.returnNote" label="退货原因"></el-table-column>
        <el-table-column prop="orderReturn.processInformation" label="通过原因"></el-table-column>
        <el-table-column prop="shopUser.phone" label="退货人手机号"></el-table-column>
        <el-table-column prop="goodsAmount" label="金额（元）" ></el-table-column>
        <el-table-column prop="phone" label="收货人手机号"></el-table-column>
        <el-table-column label="收货人地址">
          <template slot-scope="scope">
            {{initAddressInfo(scope.row)}}
          </template>
        </el-table-column>
        <el-table-column prop="postscript" label="订单附言"></el-table-column>
        <el-table-column prop="payTime" label="支付时间"></el-table-column>
        <el-table-column prop="deliveryTime" label="收货时间"></el-table-column>
        <el-table-column fixed="right" width="225" label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              :disabled="buttonDisabled(scope.row.orderStatus, 1)"
              @click="handleInfo(scope.$index, scope.row)">详情</el-button>
            <el-button
              size="mini"
              type="success"
              :disabled="buttonDisabled(scope.row.orderStatus, 1)"
              @click="handleSuccess(scope.$index, scope.row)">完成</el-button>
            <el-button
              size="mini"
              type="warning"
              :disabled="buttonDisabled(scope.row.orderStatus, 1)"
              @click="handleFail(scope.$index, scope.row)">失败</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 35, 50]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="goodsTotal">
        </el-pagination>
      </div>
    </div>

    <!-- 详细信息 -->
    <div>
      <el-dialog :title="orderInfoDialogTitle" :visible.sync="dialogOrderInfoVisible" width="65%">
        <span>
          <order-info v-if="dialogOrderInfoVisible" :message="orderAllInfo"></order-info>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="dialogOrderInfoVisible = false">返 回</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import orderApi from '@/api/backstage/orderReturn'
  import OrderInfo from '../components/orderInfo'
  export default {
    name: 'Goods_listInfo',
    components: {
      OrderInfo
    },
    data() {
      return {
        /** 筛选条件 **/
        filterOrderList:{
          orderSn:'',
          phone:'',
        },
        /** 日期选择器 **/
        datePickerValue:'',
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now() - 8.64e6
          }
        },
        /** 订单信息 **/
        orderInfos:[],
        currentPage: 1,
        pagesize:20,
        goodsTotal:0,
        /** 订单状态 **/
        initStatus:[80],
        /** 详细信息不显示 **/
        dialogOrderInfoVisible: false,
        /** 发货页面 **/
        dialogOrderNextVisible:false,
        /** 订单详细信息 **/
        orderAllInfo:{
          goodsAmount:'',
          shopUser:{phone:''},
          consignee:'',
          phone:'',
          province:'',
        },
        /** 订单详情显示标题 **/
        orderInfoDialogTitle:'',
      }
    },
    created() {
      this.initData();
    },
    methods: {
      initData() {
        let params = {
          orderStatusList: this.initStatus,
          currentPage: this.currentPage,
          pagesize: this.pagesize,
          orderSn: this.filterOrderList.orderSn,
          phone: this.filterOrderList.phone,
          createDate:this.datePickerValue
        }
        orderApi.initOrderList(params).then(res => {
          this.orderInfos = res.data.records;
          this.goodsTotal = res.data.total;
        }).catch(() => {
          this.$notify.error({title: '失败', message: "获取用户信息失败"});
        })
      },
      /** 分页---开始 **/
      handleSizeChange(val) {
        this.pagesize = val;
        this.initData();
      },
      handleCurrentChange(val) {
        this.currentPage = val;
        this.initData();
      },
      /** 分页---结束 **/
      /** 详细地址信息 **/
      initAddressInfo(row){
        return row.province+row.city+row.district+row.street+"-"+row.address;
      },
      /** 筛选订单编号数字 **/
      validateOrderSnRule(rule, value, callback){
        let orderSNRule = /^[0-9]*$/;
        if (!orderSNRule.test(value)) {
          console.log(value);
          callback(new Error('只能由数字组成'));
        } else {
          callback()
        }
      },
      /** 重置搜索 **/
      restOrderInfosButton(){
        this.filterOrderList.orderSn = '';
        this.datePickerValue = null;
        this.filterOrderList.orderSn = '';
        this.filterOrderList.phone = '';
        this.$refs['filterOrderList'].clearValidate();
        this.currentPage = 1;
        this.initData();
      },
      /** 条件筛选 **/
      selectOrderInfosButton(){
        this.$refs['filterOrderList'].validate(valid => {
          if (valid){
            this.currentPage = 1;
            this.initData();
          } else {
            this.$notify({title: '提示', message: '输入的信息不合规', type: 'warning'});
          }
        });
      },

      /** 操作按钮点击事件 **/
      handleInfo(orderIndex, row){
        /** 订单详情 **/
        this.orderAllInfo = {};
        this.orderAllInfo = row;
        /** 订单详情标题 **/
        this.orderInfoDialogTitle = "订单号："+row.orderSn+" - 时间："+row.createDate+" - 状态：待发货";
        /** 详细信息显示 **/
        this.dialogOrderInfoVisible = true;
      },
      /** 操作按钮显示 **/
      buttonDisabled(status, type){
      },
      /** 通过事件 **/
      handleSuccess(orderIndex, row){
        this.$confirm('退货确定完成?', '完成', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          orderApi.returnProcessingSuccOrFail({id:row.id,orderStatus:90}).then(res => {
            this.$notify.success({title: '成功', message: '退货成功!'});
            this.initData();
          }).catch(() => {
            this.$notify.error({title: '失败', message: '退货成功操作失败，请稍后再试!'});
          })
        }).catch(() => {
          this.$notify.info({title: '取消', message: '已取消退货成功操作'});
        });
      },
      handleFail(orderIndex, row){
        this.$confirm('退货失败?', '失败', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          orderApi.returnProcessingSuccOrFail({id:row.id,orderStatus:95}).then(res => {
            this.$notify.success({title: '成功', message: '用户退货失败操作成功!'});
            this.initData();
          }).catch(() => {
            this.$notify.error({title: '失败', message: '退货失败操作异常，请稍后再试!'});
          })
        }).catch(() => {
          this.$notify.info({title: '取消', message: '已取消退退货失败操作'});
        });
      },
    }
  }
</script>

<style>
  .licc-order-return-process-top{
    width: 100%;
    min-width: 1100px;
  }
  .licc-order-return-process-form>.el-form-item{
    margin-bottom: 0px;
  }
</style>
